package com.gba.mark.exam.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.gba.mark.exam.entity.ExamPaper;
import com.gba.mark.exam.entity.ExamQuestions;
import com.gba.mark.exam.mapper.ExamPaperMapper;
import com.gba.mark.exam.mapper.ExamQuestionsMapper;
import com.gba.mark.exam.service.ExamPaperService;
import com.gba.mark.exam.service.ExamQuestionsService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class ExamPaperImpl extends ServiceImpl<ExamPaperMapper, ExamPaper> implements ExamPaperService {

    @Resource
    private ExamPaperMapper examPaperMapper;

    @Autowired
    private ExamPaperService examPaperService;

    @Autowired
    private ExamQuestionsService examQuestionsService;

   /**
   *保存并更新一份试卷
    */
    @Override
    public int savePaper(List<ExamQuestions> questions, int group_id) {

        JSONObject jb = new JSONObject();
        int index = 1;
        for (ExamQuestions question : questions) {
            examQuestionsService.saveQuestion(question);
            int question_ids = question.getId();
            jb.put(String.valueOf(index++), question_ids);
        }

        ExamPaper examPaper = new ExamPaper();
        examPaper.setQuestion_ids(jb);
        examPaper.setGroup_id(group_id);
        return this.baseMapper.insert(examPaper);
    }

    /**
    *删除一份试卷
     */
    @Override
    public boolean removeById(Serializable id) {
        //删除对应的试卷类型数据
        ExamPaper basePaper = this.baseMapper.selectById(id);
        //当删除试卷时也从试卷表中删除相应的内容
        examPaperService.remove(new QueryWrapper<ExamPaper>().eq("id", String.valueOf(id)));
        return true;

    }

    /**
     * 给新入组的用户展示试卷
     */

//    @Override
//    public int showPaper(int group_id){
//
//
//        //在Mapper中写入查询方法
//
//        //return
//    }



//    /**
//    *查询一份试卷
//     */
//    @Override
//    public List<ExamQuestions> findPaper(Integer id, Integer group_id, Integer question_ids) {
//
//
//        return examPaperMapper.findPaper(id, group_id, question_ids);
//
//
//    }
}

